package operate;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletRequest;
import util.Log;
import util.Page;
import util.StrUtil;
import db.JdbcOperate;

public class DataOperate {
	/**@Desc查询某个表单所有记录
	 * @param page
	 * @param id 表单id
	 * @return
	 */
	public static Page findDataById(Page page, String id) {
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t" + id);
		try {
			return JdbcOperate.getPageNew(page, sb.toString());
		} catch (SQLException e) {
			Log.warn(DataOperate.class, "查询表单数据", e);
		}
		return null;
	}
	/**@Desc 插入表单数据
	 * @param request
	 */
	public static void addData(ServletRequest request){
		try {
			JdbcOperate.insertOrUpdate(createInsertSQL(request));
		} catch (SQLException e) {
			Log.warn(DataOperate.class, "插入表单数据", e);
		}
	}
	public static void updateData(ServletRequest request) {
		try {
			JdbcOperate.insertOrUpdate(createUpdateSQL(request));
		} catch (SQLException e) {
			Log.warn(DataOperate.class, "更新表单数据", e);
		}
		
	}
	/**@Desc生成表单更新数据sql语句
	 * @param request
	 * @return   
	 */
	public static String createUpdateSQL(ServletRequest request) {
		StringBuffer buffer=new StringBuffer();
		StringBuffer keyValue=new StringBuffer();
		Map map = request.getParameterMap();
		Set entries=map.entrySet();
		String cid=request.getParameter("cid");//表id
		String id=request.getParameter("id");//数据id
		buffer.append("update t").append(cid).append(" set ");
		if(null!=entries){
			Iterator iterator=entries.iterator();
			while(iterator.hasNext()){
				Map.Entry	entry=(Map.Entry)iterator.next();
				Object key=entry.getKey();
				Object value=entry.getValue();
				String keyval = "";
				if(key.equals("cid")||key.equals("method")||key.equals("id")){
					continue;//有cid就直接跳过
				}
				String[] values = new String[1];
				if (value instanceof String[]) {
					values = (String[]) value;
				} else {
					values[0] = value.toString();
				}
				keyValue.append(",f").append(key).append("='").append(values[0]);
				for(int i=1,length=values.length;i<length;i++){
					keyValue.append(":").append(values[i]);
				}
				keyValue.append("'");
			}
		}	
		buffer.append(keyValue.toString().substring(1));
		buffer.append(" where id="+id);
		return buffer.toString();
	}
	
	/**@Des 生成表单插入数据sql语句
	 * @param request
	 * @param table
	 * @return
	 */
	private static String createInsertSQL(ServletRequest request) {
		StringBuffer val=new StringBuffer();
		StringBuffer keys=new StringBuffer();
		StringBuffer SQLS=new StringBuffer();
		Map map = request.getParameterMap();
		String cid=request.getParameter("cid");
		Set entries=map.entrySet();
		if(null!=entries){
			Iterator iterator=entries.iterator();
			while(iterator.hasNext()){
				Map.Entry	entry=(Map.Entry)iterator.next();
				Object key=entry.getKey();
				Object value=entry.getValue();//
				String keyval = "";
				if(key.equals("cid")||key.equals("method")){
					continue;//有cid或者method关键字就直接跳过
				}
				String[] values = new String[1];
				if (value instanceof String[]) {
					values = (String[]) value;
				} else {
					values[0] = value.toString();
				}
				keys.append(",").append("f").append(key);
				val.append(",").append("'").append(values[0]);
				for(int i=1,length=values.length;i<length;i++){
					val.append(":").append(values[i]);
				}
				val.append("'");
			}
			SQLS.append("insert into t").append(cid)
			.append("(id,")
			.append(StrUtil.startAtSecode(keys.toString()))
			.append(")values(t"+cid+"_seq.nextval,")
			.append(val.toString().substring(1))
			.append(")");
		}
		return SQLS.toString();
	}
}
